﻿using System;
using System.Collections.Generic;

namespace VisitorPattern;

// 定义Visitor接口
public interface IVisitor
{
    void Visit(Monkey monkey);
    void Visit(Elephant elephant);
}

// 定义Element抽象类
public abstract class Animal
{
    public abstract void Accept(IVisitor visitor);
}

// 具体Element类：Monkey
public class Monkey : Animal
{
    public override void Accept(IVisitor visitor)
    {
        visitor.Visit(this);
    }
}

// 具体Element类：Elephant
public class Elephant : Animal
{
    public override void Accept(IVisitor visitor)
    {
        visitor.Visit(this);
    }
}

// 具体的Visitor类：FeedingVisitor
public class FeedingVisitor : IVisitor
{
    public void Visit(Monkey monkey)
    {
        Console.WriteLine("给猴子投喂香蕉");
    }

    public void Visit(Elephant elephant)
    {
        Console.WriteLine("给大象投喂青草");
    }
}

// 具体的Visitor类：CleaningVisitor
public class CleaningVisitor : IVisitor
{
    public void Visit(Monkey monkey)
    {
        Console.WriteLine("清理猴子的树屋");
    }

    public void Visit(Elephant elephant)
    {
        Console.WriteLine("清理大象的围栏");
    }
}

// ObjectStructure类：Zoo
public class Zoo
{
    private readonly List<Animal> _animals = [];

    public void AddAnimal(Animal animal)
    {
        _animals.Add(animal);
    }

    public void Accept(IVisitor visitor)
    {
        foreach (var animal in _animals)
        {
            animal.Accept(visitor);
        }
    }
}

// 客户端代码
public static class Program
{
    public static void Main()
    {
        Zoo zoo = new Zoo();
        zoo.AddAnimal(new Monkey());
        zoo.AddAnimal(new Elephant());

        FeedingVisitor feedingVisitor = new FeedingVisitor();
        zoo.Accept(feedingVisitor);

        CleaningVisitor cleaningVisitor = new CleaningVisitor();
        zoo.Accept(cleaningVisitor);
    }
}